# Maintainer: Kyle Keen <keenerd@gmail.com>
# Contributor: Dominik Heidler <dheidler@gmail.com>

# it looks like the release model has gotten better
# keeping it more modular for now though

pkgname=bladerf
pkgver=2023.02
_FPGAver=0.15.0
_firmver=2.4.0
_noOScommit=0bba46e  # upstream has pinned a submodule to this
pkgrel=2
pkgdesc="Driver, userspace, fpga & firmware for the bladeRF SDR."
url="https://github.com/Nuand/bladeRF"
# https://github.com/Nuand/bladeRF/tags
arch=('loong64' 'x86_64')
license=('GPL2')
depends=('libusb')
makedepends=('cmake' 'help2man' 'doxygen')
#backup=("etc/bladerf.cfg")

source=(
    "bladerf-$pkgver.tgz::https://github.com/Nuand/bladeRF/archive/refs/tags/$pkgver.tar.gz"
    "noOS-$_noOScommit.tgz::https://github.com/analogdevicesinc/no-OS/archive/$_noOScommit.tar.gz"
    "hostedxA4-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedxA4.rbf"
    "hostedxA9-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedxA9.rbf"
    "hostedx40-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedx40.rbf"
    "hostedx115-$_FPGAver.rbf::https://nuand.com/fpga/v$_FPGAver/hostedx115.rbf"
    "https://www.nuand.com/fx3/bladeRF_fw_v$_firmver.img")

md5sums=('bc97f35923f01237c00a2036a2bb6611'
         '2c06ff9297d8beb0482a1b0b5e4d3128'
         'b61726a55f340af0f1db5fb6567f8133'
         '20a34d1b97845f5f2c51d46208e30cf4'
         '8e7593910d0eeefe52d163ed8797a337'
         '29eb852ed27eee29e9d354c9421d3fdc'
         '337f570f2e3fdb26abde0f0765196d92')
sha256sums=('3bbac54ad7d6e35be31eb12393be5e7102a070fb1ddc176992d64a6a623670c7'
            'b77a86780e82ff592dd3cbcbf916d6f421f5cb7b523b800255483b1992aea88d'
            'ef94df12726d348ad5b2a9c999063408a952695f91991461775af8f3a26018fd'
            'c6ab763460f36ee2aaf9b950d0b0e008519e84051e5e38a4e486fd04103bb974'
            '2fee0764fdec78763ad64d20dde8cbe1e4fb7c57e2c256afb0125577d3e9e45f'
            '0d5c6c033a4338623ed3eeadfaad7d23de20dcfed8203c3ae78c4e534cdec3be'
            '670d1ca7aa1c6007eb09900351c3aa997e4be316dbc0a2fc1532a908d02cc0a9')

# bladeRF-cli is bundled with libbladeRF
# bladeRF-cli can use libtecla (aur)?

prepare() {
  cd "$srcdir/bladeRF-$pkgver"
  # FS#47168
  sed -i 's|MODE.*$|TAG+="uaccess"|' host/misc/udev/*.rules.in

  # todo, report warning
  sed -i '94i add_definitions(-Wno-error=format-truncation)' host/CMakeLists.txt

  # FS#55326
  sed -i '44s/^else/#&/' host/misc/udev/CMakeLists.txt

  # manually replace the submodules
  # because "git submodule init/update" doesn't work with a release tarball
  rmdir --ignore-fail-on-non-empty thirdparty/analogdevicesinc/no-OS
  ln -s "$srcdir/no-OS-$_noOSver"* thirdparty/analogdevicesinc/no-OS
}

build() {
  cd "$srcdir/bladeRF-$pkgver/host"
  mkdir -p build
  cd build
  CFLAGS+=" -Wno-error=stringop-truncation" cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DINSTALL_UDEV_RULES=ON \
    -DBUILD_DOCUMENTATION=ON \
    -DCMAKE_INSTALL_LIBDIR:PATH=lib \
    -Dusb_LIBRARY:FILEPATH=/usr/lib/libusb-1.0.so \
    -DCMAKE_INSTALL_PREFIX=/usr ../ \
    -DUDEV_RULES_PATH=/usr/lib/udev/rules.d
  make
  # ENABLE_FX3_BUILD needs cypress toolchain
  # really really wants to put files in /usr/lib64
}

package() {
  cd "$srcdir/bladeRF-$pkgver/host/build"
  make DESTDIR="${pkgdir}" install
  cd "$srcdir"
  install -Dm644 "bladeRF_fw_v$_firmver.img" "$pkgdir/usr/share/bladerf/firmware/bladeRF_fw_v$_firmver.img"
  install -Dm644 hostedxA4-$_FPGAver.rbf     "$pkgdir/usr/share/bladerf/fpga/hostedxA4.rbf"
  install -Dm644 hostedxA9-$_FPGAver.rbf     "$pkgdir/usr/share/bladerf/fpga/hostedxA9.rbf"
  install -Dm644 hostedx40-$_FPGAver.rbf     "$pkgdir/usr/share/bladerf/fpga/hostedx40.rbf"
  install -Dm644 hostedx115-$_FPGAver.rbf    "$pkgdir/usr/share/bladerf/fpga/hostedx115.rbf"

  # FS#54105
  cd "$pkgdir/usr/lib/udev/rules.d/"
  mv 88-nuand-bladerf1.rules   70-nuand-bladerf1.rules
  mv 88-nuand-bladerf2.rules   70-nuand-bladerf2.rules
  mv 88-nuand-bootloader.rules 70-nuand-bootloader.rules
}
